package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.TradeDetailsDealAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.DetailsFilterWindow;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.DealDetailsInfo;
import com.lanlian.commonlib.bean.ReqDealDetails;
import com.lanlian.commonlib.bean.RespDealDetails;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.OnEventID;
import com.lanlian.commonlib.util.OnEventUtils;
import com.lanlian.utillib.DateUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 16:51
 *     desc   : 成交明细
 *     version: 1.0
 * </pre>
 */
public class TradeDetailsDealActivity extends BaseActivity implements View.OnClickListener {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;
    private ArrayList<DealDetailsInfo> mList = new ArrayList<>();
    private DetailsFilterWindow mPopupWindow;
    private TradeDetailsDealAdapter mAdapter;

    private String mBeginDate;
    private String mEndDate;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trade_details_deal, true);
        initView();
        setListeners();
        initAdapter();
        mLayRefresh.autoRefresh();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("成交明细");
        setRightImage(R.mipmap.detailed_icon_screen);
        mRecycleView = findViewById(R.id.recycle_view);
        mLayRefresh = findViewById(R.id.lay_refresh);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mImgAction.setOnClickListener(this);
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mRecycleView.scrollToPosition(0);
                getDealDetails();
            }
        });
    }

    private void initAdapter() {
        mAdapter = new TradeDetailsDealAdapter(R.layout.adapter_trade_details_deal, mList);
        mRecycleView.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecycleView.setLayoutManager(linearLayoutManager);
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.lay_empty_details);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                OnEventUtils.onEvent(TradeDetailsDealActivity.this, OnEventID.PNL_DETAIL);
                startActivity(new Intent(TradeDetailsDealActivity.this, TradeDetailsPnlActivity.class)
                        .putExtra("dealNo", mList.get(position).getlDealNo()));
            }
        });
    }

    private void getDealDetails() {
        ReqDealDetails reqDealDetails = new ReqDealDetails();
        reqDealDetails.setBeginDate(mBeginDate);
        reqDealDetails.setEndDate(mEndDate);
        VolleyRequestUtils.getInstance().getDealDetails(this.toString(), reqDealDetails,
                new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        mLayRefresh.finishRefresh();
                        RespDealDetails respDealDetails = (RespDealDetails) response;
                        mList = respDealDetails.getDataList();
                        mAdapter.setNewData(mList);
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        mLayRefresh.finishRefresh();
                        showToast(baseResponse.getMsg());
                    }
                });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.img_action:
                if (mPopupWindow == null) {
                    mPopupWindow = new DetailsFilterWindow(this, new DetailsFilterWindow.OnFilterListener() {
                        @Override
                        public void onFilter(long beginDate, long endDate) {
                            mBeginDate = DateUtils.longToString(beginDate, "yyyy-MM-dd 00:00:00");
                            mEndDate = DateUtils.longToString(endDate, "yyyy-MM-dd 24:00:00");
                            mLayRefresh.autoRefresh();
                        }
                    });
                }
                mPopupWindow.showPopWindow(v, 0, 0);
                break;
            default:
                break;
        }
    }
}
